package com.ghsc.ghxc.jsb.ui.live.view.cust

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebSettings
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.cn.appcore.ext.loge
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.startKtxActivity
import com.cn.appcore.utils.GlideUtil
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.databinding.ImCustSysOrderViewBinding
import com.ghsc.ghxc.jsb.ui.message.bean.ImSysCustOrderBean
import com.ghsc.ghxc.jsb.ui.my.activity.OrderDetailActivity
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.netease.yunxin.kit.chatkit.ui.databinding.ChatBaseMessageViewHolderBinding
import com.netease.yunxin.kit.chatkit.ui.model.ChatMessageBean
import com.netease.yunxin.kit.chatkit.ui.normal.view.message.viewholder.NormalChatBaseMessageViewHolder
import com.netease.yunxin.kit.common.ui.utils.TimeFormatUtils

/**
 * 项目：GuoHuaMall
 * 包名：com.ghsc.ghxc.ui.message.yunxin.view
 * 类名：CusRedAttachmentViewHolder
 * 创建时间：2023/10/24 21:10
 * 创建人 @author： xiaolin
 * 描述 ：Todo...自定义消息。订单消息
 */
class CusSysOrderAttachmentViewHolder(parent: ChatBaseMessageViewHolderBinding, viewType: Int) : NormalChatBaseMessageViewHolder(
    parent, viewType
) {

    private var binding //创建自己的layout布局
            : ImCustSysOrderViewBinding? = null
    override fun addViewToMessageContainer() {
        binding = ImCustSysOrderViewBinding.inflate(
            LayoutInflater.from(parent.context), messageContainer, true
        )
    }

    //去掉背景
    override fun onMessageBackgroundConfig(messageBean: ChatMessageBean?) {
        baseViewBinding.messageContainer.setBackgroundResource(R.color.title_transfer)
    }



    override fun bindData(message: ChatMessageBean?, lastMessage: ChatMessageBean?) {
        super.bindData(message, lastMessage)
        currentMessage = message
        val bean = message!!.messageData.message.attachment as CusSysOrderAttachment
            ?: return

        binding?.let {
//            val para1 = it.llItem.layoutParams
//            para1.width =
//                StringUtil.dp2px((StringUtil.screenWidth() / StringUtil.screenDensity())-50).toInt()
//            it.llItem.layoutParams = para1


            val layoutParams = it.llItem.layoutParams
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
            it.llItem.layoutParams = layoutParams
            it.topTitle.text=bean.top_title
            it.mainTitle.text=bean.main_title
            // 获取待展示消息时间戳，若为 0 则设置为当前系统时间
            val createTime =
                if (message.messageData.message.time == 0L) System.currentTimeMillis() else message.messageData.message.time
            it.crtTime.text =
                TimeFormatUtils.formatMillisecond(parent.context, createTime)
            GlideUtil.ImageLoad(parent.context,bean.img,it.img)
            if (!TextUtils.isEmpty(bean.bottom)){
                val listData :MutableList<ImSysCustOrderBean> =
                    Gson().fromJson(bean.bottom, object : TypeToken<MutableList<ImSysCustOrderBean>>() {}.type)
                if (listData.isNotEmpty()) {
                    val mAdapter: OrderItemListAdapter by lazy { OrderItemListAdapter() }
                    var mList = arrayListOf<ImSysCustOrderBean>()
                    it.rvList.layoutManager = LinearLayoutManager(parent.context)
                    it.rvList.adapter = mAdapter
                    mAdapter.replaceData(listData)
                    it.rvList.visibility=View.VISIBLE
                } else  it.rvList.visibility=View.GONE
            }else it.rvList.visibility=View.GONE


            loge("订单==="+bean.oid +"top_title="+bean.top_title+"main_title="+bean.main_title+"img="+bean.img+"bottom="+bean.bottom)
            it.llItem.setSingleClickListener {
                parent.context.startKtxActivity<OrderDetailActivity>(
                    value = "oid" to bean.oid.toString()
                )
            }

            //
            it.webView.visibility=View.VISIBLE
            val webSettings: WebSettings = it.webView.settings
            webSettings.javaScriptEnabled = true //允许使用js
            //设置WebView是否支持使用屏幕控件或手势进行缩放，默认是true，支持缩放
            webSettings.setSupportZoom(false)
            //设置WebView是否使用其内置的变焦机制，该机制集合屏幕缩放控件使用，默认是false，不使用内置变焦机制
            webSettings.builtInZoomControls = false
            //置WebView使用内置缩放机制时，是否展现在屏幕缩放控件上，默认true，展现在控件上
            webSettings.displayZoomControls = false

        }


    }

    inner class OrderItemListAdapter   : BaseQuickAdapter<ImSysCustOrderBean, BaseViewHolder>(
        R.layout.cust_rv_order_list_item) {

        override fun convert(helper: BaseViewHolder, item: ImSysCustOrderBean) {
            helper.setText(R.id.tvKey,item.title)
                .setText(R.id.tvValue,item.value.toString())

        }
    }
}